import utils.jdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

@WebServlet("/index")
public class Index extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        session.setAttribute("title","我是首页");
        String username=req.getParameter("username");
        req.setAttribute("username",username);
        String str="select * from booklist";
        PreparedStatement ps=null;
        Connection connection= jdbcUtils.getConnection();
        HashMap<String,String> map=new HashMap();
        try {
            ps=connection.prepareStatement(str);
            ResultSet rs =ps.executeQuery();
            while (rs.next()){
                String id= String.valueOf(rs.getInt(1));
                map.put(id,rs.getString(2));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        jdbcUtils.Colse(connection);
        req.setAttribute("map",map);
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
    }
}
